package com.mew.common.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import java.util.Date;


public class DateUtils extends DateUtil {

    //true 表示过期了， false表示还没有过期
    public static boolean outOfDateBySeconds(Long date, int seconds){
        Long time = org.apache.commons.lang3.time.DateUtils.addSeconds(new Date(date), seconds).getTime();
        Long nowTime = new Date().getTime();
        return nowTime > time ? true : false;
    }


    /**
     * 计算剩余时间
     * @param startTime
     * @param durationTime
     * @return
     */
    public static int remainTime(long startTime, int durationTime, DateUnit unit){
        System.out.println("--------------------------------------------------");
        System.out.println(System.currentTimeMillis()   );
        System.out.println( startTime  );
        System.out.println(System.currentTimeMillis() - startTime  );
        long rt = (System.currentTimeMillis() - startTime) / unit.getMillis();
        long time = durationTime - rt;
        System.out.println(durationTime);
        System.out.println(rt);
        System.out.println(time);
        return time < 0 ? 0 : (int) time;
    }

    public static int remainTimeMillis(long startTime, int durationTime){

        long rt = System.currentTimeMillis() - startTime;

        long time = durationTime - rt;

        return time < 0 ? 0 : (int) time;
    }

    public static int betweenByLong(long begin, long end, DateUnit unit, boolean isAbs){
        if (begin == 0 || end == 0){
            return 1024;
        }

        long diff = isAbs && begin > end ? begin - end : end - begin;
        return (int) (diff / unit.getMillis());
    }

    public static long getTodayDate(){
        String today = DateUtils.format(new Date(), "yyyy-MM-dd");
        return DateUtil.parse(today, "yyyy-MM-dd").getTime();
    }

    public static Date getNowDate(){
        String dateForm = DateUtils.format(new Date(), "yyyy-MM-dd");
        return DateUtil.parse(dateForm, "yyyy-MM-dd").toJdkDate();
    }

    /**
     * 偏移到指定天数后的指定时间
     * 例如： H：6 offsetDay:3
     *      偏移到三天后的六点整
     * @param H
     * @param offsetDay
     * @return
     */
    public static Date getOffsetTimeDate(int H,int offsetDay){
        return DateUtil.parse(DateUtil.today()+" "+H+":00:00").offset(DateField.DAY_OF_MONTH,offsetDay);
    }

    /**
     * 获取当天时间的龙
     * @return
     */
    public static long getNowDayLong(){
        return DateUtil.parse(DateUtil.today()+" 00:00:00").getTime();
    }

    public static void main(String[] args) throws Exception {
//        DateTime startDate = DateUtil.parse("2021-01-15 00:00:00", "yyyy-MM-dd HH:mm:ss");
//        DateTime startDate2 = DateUtil.parse("2021-01-15 00:01:00", "yyyy-MM-dd HH:mm:ss");
//        DateTime startDate3 = DateUtil.parse("2021-01-15", "yyyy-MM-dd");
//        System.out.println("xxxxxxxxxx"+ betweenByLong(startDate3.getTime(), System.currentTimeMillis(), DateUnit.DAY, false));
        /*String test =  "127.0.0.1@TGDC_258212411";
        String key = "1908123456789099";

        String en = StringUtils.encrypt(test, key);
        System.out.println(en);
        System.out.println(StringUtils.decrypt(en, key));*/
        long d = DateUtils.getNowDayLong();
        System.out.println(d);
        System.out.println(new Date(d));
    }
}
